package jgl.vajra.driver.widget.giide;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import jgl.vajra.driver.R;
import jgl.vajra.driver.widget.utils.DisplayUtils;
import jgl.vajra.driver.widget.utils.UIUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import org.greenrobot.greendao.annotation.NotNull;

import java.io.File;


public class GlideImageLoader {

    private static GlideImageLoader _singleInstance = null;


    public static void onDisplayGifUrl(final ImageView imageView, String url) {
        onDisplayGifUrl(null, imageView, url);

    }

    public static void onDisplayGifUrl(Context context, final ImageView imageView, String url) {
        RequestOptions options = new RequestOptions()
                .override(240, 240)
                .error(R.drawable.ic_image_error)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .placeholder(R.drawable.ic_image_loading)
                .fitCenter()
                .dontAnimate();
        Glide.with(imageView.getContext())
                .load(Uri.parse(url))
                .apply(options)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                        if (drawable instanceof GifDrawable) {
                            GifDrawable gifDrawable = (GifDrawable) drawable;
                            gifDrawable.setLoopCount(2);
                            imageView.setImageDrawable(drawable);
                            gifDrawable.start();
                        }
                    }
                });

    }

    public static void onDisplayGifUrl(final ImageView imageView, int url) {
        RequestOptions options = new RequestOptions()
                .override(240, 240)
                .error(R.drawable.ic_image_error)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .placeholder(R.drawable.ic_image_loading)
                .fitCenter()
                .dontAnimate();
        Glide.with(imageView.getContext())
                .load(url)
                .apply(options)
                .into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                        if (drawable instanceof GifDrawable) {
                            GifDrawable gifDrawable = (GifDrawable) drawable;
                            gifDrawable.setLoopCount(2);
                            imageView.setImageDrawable(drawable);
                            gifDrawable.start();
                        }
                    }
                });

    }


    public static void onDisplayImage(final ImageView imageView, @NotNull String imgUrl) {
        if (imageView == null) {
            return;
        }

        Glide.with(imageView.getContext()).load(imgUrl)//
                .thumbnail(0.55f)
                .apply(getRequestOptions())
                .into(imageView);


    }

    public static RequestOptions getRequestOptions() {
        return getRequestOptions(R.drawable.ic_banner_loading, R.drawable.ic_banner_error);
    }

    public static RequestOptions getRequestOptions(int placeholder, int errorImage) {
        return new RequestOptions()
                .error(errorImage)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .placeholder(placeholder)
                .centerCrop()
                .dontAnimate();
    }

    public static void onDisplayImage(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull File _file) {
        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext()).load(_file)//
                .apply(getRequestOptions())
                .into(imageView);
    }

    public static void onDisplayImage(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull String url, RequestListener listener) {
        if (imageView == null) {
            return;
        }
        Glide.with(context).load(url)//
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .centerCrop()
                        .dontAnimate()
                )
                .listener(listener)
                .into(imageView);
    }


    public static void onDisplayImage(final ImageView imageView, String resource, int placeholder) {
        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext()).load(resource)//
                .apply(getRequestOptions(placeholder, R.drawable.ic_image_error))
                .thumbnail(0.55f)
                .into(imageView);
    }

    public static void onDisplayImage(final ImageView imageView, String resource, int placeholder, int errorImage) {
        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext()).load(resource)//
                .apply(getRequestOptions(placeholder, errorImage))
                .thumbnail(0.55f)
                .into(imageView);
    }

    public static void onDisplayImage(final ImageView imageView, File resource, int placeholder, int errorImage) {
        if (imageView == null) {
            return;
        }

        Glide.with(imageView.getContext()).load(resource)//
                .apply(getRequestOptions(placeholder, errorImage))
                .thumbnail(0.55f)
                .into(imageView);
    }


    public static void onBigGoodsImageDisplay(final ImageView imageView, String resource) {
        onDisplayImage(imageView, resource, R.drawable.ic_big_image_loading, R.drawable.ic_big_image_error);
    }


    public static void onDisplayRound(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull String path) {

        if (imageView == null) {
            return;
        }

        Glide
                .with(imageView.getContext())
                .load(path)
                .apply(getRequestOptions().transform(new GlideRoundTransform(context)))

                .into(imageView);
    }

    public static void onDisplayRound(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull File path) {

        if (imageView == null) {
            return;
        }

        Glide
                .with(imageView.getContext())
                .load(path)
                .apply(getRequestOptions().transform(new GlideRoundTransform(context)))
                .into(imageView);
    }

    public static void onDisplayRound(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull File path, RequestListener listener) {

        if (imageView == null) {
            return;
        }

        Glide
                .with(imageView.getContext())
                .load(path)
                .apply(getRequestOptions().transform(new GlideRoundTransform(context)))
                .listener(listener)
                .into(imageView);
    }


    public static void onDisplayRound(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull String resouce, int placeholder, int error, RequestListener listener) {

        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext())
                .load(resouce)
                .apply(getRequestOptions(placeholder, error).transform(new GlideRoundTransform(context)))
                .listener(listener)
                .into(imageView);
    }

    public static void onDisplayRound(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull String resouce, int placeholder, int error) {

        if (imageView == null) {
            return;
        }
        Glide.with(imageView.getContext())
                .load(resouce)
                .apply(getRequestOptions(placeholder, error).transform(new GlideRoundTransform(context)))
                .into(imageView);
    }

    public static void onDisplayRoundedCorners(@NotNull final Context context, @NotNull final ImageView imageView, @NotNull String resouce, int placeholder, int error) {

        if (imageView == null) {
            return;
        }

        Glide.with(imageView.getContext())
                .load(resouce)
                .apply(getRequestOptions(placeholder, error).transforms(new BorderRoundTransformation(context, DisplayUtils.getDimension(R.dimen.dp_5), 0, DisplayUtils.getDimension(R.dimen.dp_0_5), UIUtils.getColor(R.color.gray_f2), 5)))
                .into(imageView);
    }


    public static void onAvatarDisplayRound(@NotNull final ImageView imageView, @NotNull String resouce, RequestListener listener) {
        //  onDisplayRound(context, imageView, resouce, R.drawable.ic_avatar_default, R.drawable.ic_avatar_default, (int) DisplayUtils.getDimension(R.dimen.dp_80), listener);
        int override = (int) DisplayUtils.getDimension(R.dimen.dp_50);
        RequestOptions requestOptions = new RequestOptions()
                .error(R.drawable.ic_mine_touxiang_default)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .placeholder(R.drawable.ic_mine_touxiang_default)
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .dontAnimate();
        Glide.with(UIUtils.getContext())
                .load(resouce)
                //
                .apply(requestOptions.override(override).transforms(new GlideRoundTransform(imageView.getContext()), new GlideBorderTransform(DisplayUtils.getDimension(R.dimen.dp_0_5), UIUtils.getColor(R.color.gray_f2))))
                .listener(listener)
                .into(imageView);


    }


    public static void onDisplayRadius(@NotNull final ImageView imageView, @NotNull String imgUrl, final int radius) {
        if (imageView == null) {
            return;
        }
        Glide
                .with(imageView.getContext())
                .load(imgUrl)
                .centerCrop()
                .apply(getRequestOptions().transform(new CornersTransform(UIUtils.getContext(), radius)))
                .into(imageView);


    }

    public static void onBannerDisplayImage(final ImageView imageView, String resource) {
        if (imageView == null) {
            return;
        }

        Glide
                .with(UIUtils.getContext())
                .load(resource)
                // .centerCrop()
                //R.drawable.ic_banner_loading, R.drawable.ic_banner_error
                .apply(new RequestOptions()
                        .transform(new CenterCrop(), new RoundedCorners((int) DisplayUtils.getDimension(R.dimen.dp_8))
                                //   .transform(new CenterCrop(), new RoundedCornersTransform(UIUtils.getContext(), DisplayUtils.getDimension(R.dimen.dp_8),true,true,false,false)
                        )).into(imageView);

    }

    public static void onBannerDisplayImage(final ImageView imageView, int resource) {
        if (imageView == null) {
            return;
        }

        Glide
                .with(UIUtils.getContext())
                .load(resource)
                // .centerCrop()
                //R.drawable.ic_banner_loading, R.drawable.ic_banner_error
                .apply(new RequestOptions()
                        .transform(new CenterCrop(), new RoundedCorners((int) DisplayUtils.getDimension(R.dimen.dp_8))
                                //   .transform(new CenterCrop(), new RoundedCornersTransform(UIUtils.getContext(), DisplayUtils.getDimension(R.dimen.dp_8),true,true,false,false)
                        )).into(imageView);

    }

    public static void onDisplayTopRadius(@NotNull final ImageView imageView, @NotNull String imgUrl, final int radius) {
        if (imageView == null) {
            return;
        }


        Glide
                .with(imageView.getContext())
                .load(imgUrl)
                .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCornersTransform(UIUtils.getContext(), DisplayUtils.getDimension(radius), true, true, false, false)
                )).into(imageView);


    }

    public static void onDisplayImageHeightRadius(ImageView iv, String url, float width, float height) {
        Glide.with(iv.getContext())
                .load(url)
                .apply(new RequestOptions()
                        //  .error(R.drawable.ic_banner_error)
                        // .placeholder(R.drawable.ic_banner_loading)
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                       // .skipMemoryCache(true)
                        .dontAnimate()
                        .transform(new CornersHeightTransform(R.dimen.dp_5, width, height)
                        )).into(iv);

    }

    public static void onDisplayRadius(final ImageView imageView, @NotNull File file, int radius, RequestListener<Drawable> requestListener) {
        if (imageView == null) {
            return;
        }

        RequestBuilder<Drawable> apply = Glide
                .with(UIUtils.getContext())
                .load(file)
                .apply(getRequestOptions().transform(new CornersTransform(UIUtils.getContext(), radius)));
        if (requestListener != null)
            apply.listener(requestListener);
        apply.into(imageView);
    }

    public static void onImageBlurTrans(final ImageView imageView, String url) {
        Glide.with(UIUtils.getContext())
                .load(url)
                /*   .error(Glide.with(imageView.getContext())
                          .load(R.drawable.ic_mine_avatar_bg)
                           .apply(new RequestOptions()

                                   .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                                   .skipMemoryCache(true)
                                   .dontAnimate().transform(new BlurTransformation(25, 14))
                           )
                   )*/
                .apply(new RequestOptions()
                        .skipMemoryCache(true)
                        .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                        .dontAnimate().transform(new BlurTransformation(8, 1))
                )
                .into(imageView);

    }

}
